package com.lucweb.action;


import com.alibaba.fastjson.JSON;

import com.lucweb.entity.*;
import com.lucweb.service.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller("companyAction")
@Scope("prototype")
public class CompanyAction extends ActionSupport{

    @Resource
    private CompanyService companyService;

    @Resource
    private JcService jcService;

    @Resource
    private WxService wxService;

    @Resource
    private WhyjService whyjService;

    @Resource
    private OtherService otherService;

    @Resource
    private BaseDictService baseDictService;

    @Resource
    private CallService callService;





    private File file;
    //文件名称
    private String fileFileName;

    //文件类型
    private String fileContentType;
    //注意：文件名称和文件类型的名称前缀必须相同，

    private InputStream fileInput;


    private TaskCompany taskCompany;

    private int taskId;

    private String company;

    //新建接电窗口
    public String newcompany(){

        //没有id时设置id
        int bid = companyService.getmaxId();

        ActionContext.getContext().put("bid",bid);

        return "companys";

    }

    //按名称搜索单位
    public String getCompanyList() throws IOException {

        List<TaskCompany>companyList = companyService.gettaskcompanyByName(company);

//2 将list转换为 json格式
        String json = JSON.toJSONString(companyList);

        //3 将json发送给浏览器

        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(json);
        return null;

    }

    //修改接电
    public String editcompany(){

        taskCompany = companyService.gettaskcompanyById(taskId);

        ActionContext.getContext().put("taskCompany", taskCompany);





        System.out.println(taskCompany);



        return "companys";

    }

    //删除接电
    public String deletecompany(){

        companyService.deletecompanytask(taskId);

        //刷新页面
        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "addcompany";

    }

    //接电
    public String saveCompany(){


        //先保存
        companyService.saveCompany(taskCompany);

        //刷新页面
        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "addcompany";

    }

    //接电
    public String saveOrUpdate(){


        File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
        //判断文件是否上传，如果上传的话将会创建该目录
        if(!uploadFile.exists()){
            uploadFile.mkdir(); //创建该目录
        }


        try {
            if(file!=null){
                FileUtils.copyFile(file,new File(uploadFile,fileFileName));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        if(taskCompany.getUpdown()!=null){
            if(taskCompany.getUpdown().length()==0){
                if(fileFileName!=null){
                    taskCompany.setUpdown(fileFileName);
                }else {
                    taskCompany.setUpdown(null);
                }

            }else {
                if(fileFileName!=null){
                    taskCompany.setUpdown(fileFileName);
                }

            }

        }


        System.out.println(taskCompany);


        //先保存
        companyService.saveOrUpdate(taskCompany);

        //刷新页面
        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "addcompany";

    }

    //获取单位
    public String companyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "addcompany";

    }

    //获取单位
    public String questionCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "addquestion";

    }

    //维护月检遗留单位名称
    public String whyjCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjCompanyshow";

    }

    //维护月检遗留单位名称
    public String whyjylCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjylwtfk";

    }

    //维护月检遗留单位名称
    public String bcyjljwtCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "bcyjljwtCompanyshow";

    }


    //维护月检遗留单位名称
    public String whyjylBjCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjylBjCompanyshow";

    }

    //维护月检遗留单位名称
    public String whyjylGzCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjylGzCompanyshow";

    }

    public String whyjcgGzCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjcgGzCompanyshow";

    }
    public String whyjSqCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjSqCompanyshow";

    }

    public String whyjHzCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjHzCompanyshow";

    }

    public String whyjYclCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjYclCompanyshow";

    }

    public String whyjcgBjCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "whyjcgBjCompanyshow";

    }

    //获取单位台账页面
    public String tzCompanyshow(){


        List<TaskCompany>companyList=companyService.showcompany();
        ActionContext.getContext().put("companyList",companyList);

        return "addtz";

    }

    //获取单位台账页面
    public String yjcfkCompanyshow(){

        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("companyList",companyList);

        return "addfk";

    }


    //点击单位进入会员单位页面
    public String members(){

        //通过单位名称获取单位信息
        TaskCompany taskCompany = companyService.gettaskcompanyById(taskId);
        ActionContext.getContext().put("taskCompany",taskCompany);

        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName("companyName='"+company+"'");
        ActionContext.getContext().put("taskJcList",taskJcList);


        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName("companyName='"+company+"'");
        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName("companyName='"+company+"'");
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);


        //获取其他

        List<TaskOther>taskOtherList =otherService.showOther("companyName='"+company+"'");
        ActionContext.getContext().put("taskOtherList",taskOtherList);

        //获取接电
        List<TaskCall>callList=callService.showcallbycompany("companyName='"+company+"'");
        ActionContext.getContext().put("callList",callList);

        //获取台账
        List<TaskTz>tzList=whyjService.showTz();
        ActionContext.getContext().put("tzList",tzList);



        return "members";

    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public InputStream getFileInput() {
        return fileInput;
    }

    public void setFileInput(InputStream fileInput) {
        this.fileInput = fileInput;
    }

    public CompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    public JcService getJcService() {
        return jcService;
    }

    public void setJcService(JcService jcService) {
        this.jcService = jcService;
    }

    public WxService getWxService() {
        return wxService;
    }

    public void setWxService(WxService wxService) {
        this.wxService = wxService;
    }

    public WhyjService getWhyjService() {
        return whyjService;
    }

    public void setWhyjService(WhyjService whyjService) {
        this.whyjService = whyjService;
    }

    public OtherService getOtherService() {
        return otherService;
    }

    public void setOtherService(OtherService otherService) {
        this.otherService = otherService;
    }

    public BaseDictService getBaseDictService() {
        return baseDictService;
    }

    public void setBaseDictService(BaseDictService baseDictService) {
        this.baseDictService = baseDictService;
    }

    public TaskCompany getTaskCompany() {
        return taskCompany;
    }

    public void setTaskCompany(TaskCompany taskCompany) {
        this.taskCompany = taskCompany;
    }
    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }


    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public CallService getCallService() {
        return callService;
    }

    public void setCallService(CallService callService) {
        this.callService = callService;
    }
}
